﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;

namespace Business
{
    public partial class TestCategory : Form
    {
        public MainForm m_mainform;
        public TestCategory()
        {
            InitializeComponent();
        }
        public void add(string stable, string sname)
        {
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"insert into {0} (table_2) values ('{1}')", stable, sname);
            if (!objquery.UpdateTable(ssqlex))
            {
                MessageBox.Show("添加失败");
            }
            //保存操作日记
            m_mainform.savecaozuoriji(ssqlex, "新增", "业务系统", "新增检测类别");
        }
        public void update(string stable,   string sname)
        {
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"update {0} set table_2='{1}' ", stable,  sname);
            if (!objquery.UpdateTable(ssqlex))
            {
                MessageBox.Show("修改失败");
            }
            //保存操作日记
            m_mainform.savecaozuoriji(ssqlex, "更新", "业务系统", "更新检测类别");
        }
        public void del(string stable, string sname)
        {
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"delete {0} where table_2='{1}'", stable, sname);
            if (!objquery.UpdateTable(ssqlex))
            {
                MessageBox.Show("删除失败");
            }
            //保存操作日记
            m_mainform.savecaozuoriji(ssqlex, "删除", "业务系统", "删除检测类别");
        }
        void inicbx(ListView cbx, string dataname)
        {
            cbx.Items.Clear();
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0}", dataname);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem li = new ListViewItem();
                li.SubItems[0].Text = string.Format("{0}", i + 1);
                li.SubItems.Add(dt.Rows[i]["table_2"].ToString().Trim());
                cbx.Items.Add(li);
            }
        }

       

        private void lv_SelectedIndexChanged(object sender, EventArgs e)
        {
            int k = lv.SelectedItems.Count;

            foreach (ListViewItem item in lv.SelectedItems)
            {
                textBox1.Text = item.SubItems[1].Text.ToString().Trim();
            }
        }

        private void 检测类别管理_Load(object sender, EventArgs e)
        {
            inicbx(lv, "table_jclb");
            //判断是否民警
            if (m_mainform.sfjy.Contains("检测类别管理增加"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_add.Enabled = false;
                }
            }

            //判断是否民警
            if (m_mainform.sfjy.Contains("检测类别管理删除"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_delete.Enabled = false;
                }
            }

            if (!m_mainform.squanxian.Contains("检测类别管理增加"))
            {
                but_add.Enabled = false;
            }

            if (!m_mainform.squanxian.Contains("检测类别管理编辑"))
            {
                but_config.Enabled = false;
            }

            if (!m_mainform.squanxian.Contains("检测类别管理删除"))
            {
                but_delete.Enabled = false;
            }

        }

        private void but_add_Click(object sender, EventArgs e)
        {
            
                //判断是否核心功能
            if (m_mainform.shxgn.Contains("检测类别管理增加"))
            {
                m_mainform.baocunhxrj("检测类别管理增加", string.Format("检测类别管理增加"), "写入");// 核心功能日记  日记内容
            }
            add("table_jclb", textBox1.Text.Trim());
            inicbx(lv, "table_jclb");
        }

        private void but_config_Click(object sender, EventArgs e)
        {
            update("table_jclb", textBox1.Text.Trim());
            inicbx(lv, "table_jclb");
        }

        private void but_delete_Click(object sender, EventArgs e)
        {
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("检测类别管理删除"))
            {
                m_mainform.baocunhxrj("检测类别管理删除", string.Format("检测类别管理删除"), "删除");// 核心功能日记  日记内容
            }
            del("table_jclb", textBox1.Text.Trim());
            inicbx(lv, "table_jclb");
        }
    }
}
